import { http } from "@/utils/http";
/**
 *
 * @returns 返回树形列表
 */
export function getMenuTreeList() {
  return http.get<MResponse<ShopTreeMenuVo[]>>("/user/menu/getMenuTreeList");
}

export function addMenu(data: AddMenuRequest) {
  return http.post<MResponse<number>>("/user/menu/addMenu", { data });
}

export function updateMenu(data: UpdateMenuRequest) {
  return http.post<MResponse>("/user/menu/updateMenu", { data });
}

export function delMenu(data: DelMenuRequest) {
  return http.post<MResponse>("/user/menu/delMenu", { data });
}

export const getAsyncRoutes = () => {
  return http.get<MResponse<Array<MenuVo>>>("/user/menu/getAuthMenuList").then(data => {
    if (data.code == RESPONSE_CODE.SUCCESS) {
      return { data: data.data.map(menu => buildRoute(menu)) };
    }
  });
};

function buildRoute(menu: MenuVo) {
  let route: { [x: string]: any; children?: any[] } = {
    path: menu.menuPath,
    name: menu.menuPath,
    meta: {
      title: menu.menuName,
      icon: menu.menuIcon
    }
  };
  if (menu.children?.length > 0) {
    route.children = menu.children.map(submenu => buildRoute(submenu));
  }
  return route;
}
